import pygame, sys

pygame.init()

screen = pygame.display.set_mode((576, 960))

clock = pygame.time.Clock()

# 创建图片资源
bg_surface = pygame.image.load("assets/background-day.png")
bg_surface = pygame.transform.scale2x(bg_surface)

floor_surface = pygame.image.load("assets/base.png")
floor_surface = pygame.transform.scale2x(floor_surface)

bird_surface = pygame.image.load("assets/bluebird-midflap.png")
bird_surface = pygame.transform.scale2x(bird_surface)
bird_rect = bird_surface.get_rect(center=(100, 400))

# 小鸟下坠的速度
gravity = 0.25
bird_movement = 0 # 小鸟向下移动的距离

bg_x_pos = 0
floor_x_pos = 0

while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      sys.exit()
    if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
      bird_movement = 0
      bird_movement -= 10

  # 显示图片
  # 显示背景
  screen.blit(bg_surface, (bg_x_pos, 0))
  screen.blit(bg_surface, (bg_x_pos + 576, 0))

  bg_x_pos = bg_x_pos - 0.6

  if bg_x_pos <= -576:
    bg_x_pos = 0


  # 显示小鸟
  bird_movement += gravity
  bird_rect.centery += bird_movement
  screen.blit(bird_surface, bird_rect)

  # 显示地板
  screen.blit(floor_surface, (floor_x_pos, 800))
  screen.blit(floor_surface, (floor_x_pos + 576, 800))

  floor_x_pos -= 2

  if floor_x_pos <= -576:
    floor_x_pos = 0

  pygame.display.update()

  clock.tick(60)